$(function () {

    $.ajax({
        url: BigNew.user_info,
        headers: { Authorization: localStorage.getItem('bignews_token') },
        success: function (res) {
            console.log(res.data.userPic)

        }
    })

    let src
    //获取用户信息,并显示到页面中
    function init() {
        $.ajax({
            url: BigNew.user_detail,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            success: function (res) {
                // console.log(res)
                $('#username').val(res.data.username)
                $('#nickname').val(res.data.nickname)
                $('#email').val(res.data.email)
                $('.user_pic')[0].src = res.data.userPic
                $('#password').val(res.data.password)
            }
        })
    }
    init()



    $('#userPic').on('change', function () {
        let userPic = $('#userPic')[0].files[0]
        if (userPic.type.indexOf('image/') == -1) {
            alert('只能选择图片作为封面')
            $('#userPic').val('')
            return
        }
    })
    // 用户信息修改
    $('.btn-edit').on('click', function () {
        // console.log(1)
        // let myfile = { username, nickname, email, userPic, password }
        let username = $('#username').val()
        let nickname = $('#nickname').val()
        let email = $('#email').val()
        let userPic = $('#userPic')[0].files[0]
        if (!userPic) {
            // $('#userPic').val(src)
            userPic = $('#userPic')[0].files[0]
        }
        let password = $('#password').val()
        let formdata = new FormData()
        // console.log(userPic)
        //键值对
        formdata.append('username', username)
        formdata.append('nickname', nickname)
        formdata.append('email', email)
        formdata.append('userPic', userPic)
        formdata.append('password', password)
        // if (userPic) {
        //     formdata.append('userPic', userPic)
        // }
        $.ajax({
            type: "post",
            url: BigNew.user_edit,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: formdata,
            dataType: "json",
            processData: false,
            contentType: false,
            success: function (res) {
                console.log(res)
                init()
                $('#userPic').val('')
                $.ajax({
                    url: BigNew.user_info,
                    headers: { Authorization: localStorage.getItem('bignews_token') },
                    dataType: "json",
                    success: function (res) {
                        window.parent.$('.user_info img')[0].src = res.data.userPic
                        window.parent.$('.user_info span').html('欢迎&nbsp;&nbsp;' + res.data.nickname)
                        window.parent.$('.header_bar img')[0].src = res.data.userPic
                    }

                })
                // if (res.code == 200) {
                //     alert('修改成功');
                //     /* 
                //     window:            当前页面窗口 user.html
                //     window.parent:     当前页面父窗口 index.html
                //     */
                //     location.reload()
                //     window.parent.location.reload();
                // }
                // window.location.reload()
            }
        });
    })
})